require 'erb'
require 'common/base'

class IPFRAGBase < Base

  attr_accessor :step, :transport, :base_hosts, :base_options, :base_transports, :base_variables, :message,
    :ip_src, :ip_dst, :dscp, :icmp_checksum, :icmp_reply_checksum, :more_frags_1,:more_frags_2,:more_frags_3, :offset_1,:offset_2,:offset_3, :data

  def initialize(step, filename, params = {}, transport="IPFRAG")
    super()
    @transport = transport
    @step = step

    @ip_src = params["ip_src"] or "192.168.123.1"
    @ip_dst = params["ip_dst"] or "192.168.123.2"
    @data = params["data"] or "01234567"
    @dscp = params["dscp"] or "000000"
    @icmp_checksum = params["icmp_checksum"] or "0xc4a7"
    @icmp_reply_checksum = params["icmp_reply_checksum"] or "0xcca7"
    @identifier = params["identifier"] or "1"
    @more_frags_1 = params["more_frags_1"] or "1"
    @more_frags_2 = params["more_frags_2"] or "1"
    @more_frags_3 = params["more_frags_3"] or "0"
    @offset_1 = params["offset_1"] or "0"
    @offset_2 = params["offset_2"] or "3"
    @offset_3 = params["offset_3"] or "7"

    @base_hosts ='&host_0 = host(role: "client", description: "client/src", type: l2)
        &host_1 = host(role: "server", description: "server/dst", type: l2)
    '

    @base_options = "
        $ip_src = '<%= @ip_src %>'
        $ip_dst = '<%= @ip_dst %>'
        $data = '<%= @data %>'
        $dscp = '<%= @dscp %>'
        $identifier = '<%= @identifier %>'
        $icmp_checksum = '<%= @icmp_checksum %>'
        $icmp_reply_checksum = '<%= @icmp_reply_checksum %>'
        $more_frags_1 = '<%= @more_frags_1 %>'
        $more_frags_2 = '<%= @more_frags_2 %>'
        $more_frags_3 = '<%= @more_frags_3 %>'
        $offset_1 = '<%= offset_1 %>'
        $offset_2 = '<%= offset_2 %>'
        $offset_3 = '<%= offset_3 %>'"

    @base_variables = ''

    @base_transports = 'IPFRAG = ethernet(src: &host_0, dst: &host_1, filter: "(ether dst src_mac or ether broadcast)", type: 0x0800)'

    if !filename.nil?
      template = get_message("IPFRAG", filename)
      erb = ERB.new(template, 0, '%<>')
      @message = erb.result(binding)
    end
  end
end
